package com.gitee.chessboard.jdbc.convert;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 类型转换器的抽象实现
 *
 * @author 敲代码的旺财
 * @date 2021/11/18 17:14
 */
public abstract class AbstractTypeConverter implements TypeConverter {

    /**
     * 将java类型转成数据库类型
     *
     * @param clazz java类型
     * @return 数据类型的字符串
     */
    @Override
    public String convert(Class<?> clazz) {
        if (String.class.isAssignableFrom(clazz)) {
            return convertString();
        }
        if (Date.class.isAssignableFrom(clazz)) {
            return convertDate();
        }
        if (Long.class.isAssignableFrom(clazz) || long.class.isAssignableFrom(clazz)) {
            return convertLong();
        }
        if (Integer.class.isAssignableFrom(clazz) || int.class.isAssignableFrom(clazz)) {
            return convertInteger();
        }
        if (Boolean.class.isAssignableFrom(clazz) || boolean.class.isAssignableFrom(clazz)) {
            return convertBoolean();
        }
        if (Float.class.isAssignableFrom(clazz) || float.class.isAssignableFrom(clazz)) {
            return convertFloat();
        }
        if (Double.class.isAssignableFrom(clazz) || double.class.isAssignableFrom(clazz)) {
            return convertDouble();
        }
        if (Character.class.isAssignableFrom(clazz) || char.class.isAssignableFrom(clazz)) {
            return convertCharacter();
        }
        if (BigDecimal.class.isAssignableFrom(clazz)) {
            return convertBigDecimal();
        }
        if (clazz.isEnum()){
            return convertString();
        }
        return null;
    }

    public String convertString() {
        return "VARCHAR(255)";
    }

    public String convertDate() {
        return "DATETIME";
    }

    public String convertLong() {
        return "BIGINT(20)";
    }

    public String convertInteger() {
        return "INT(10)";
    }

    public String convertBoolean() {
        return "BIT";
    }

    public String convertFloat() {
        return "FLOAT";
    }

    public String convertDouble() {
        return "DOUBLE";
    }

    public String convertCharacter() {
        return "VARCHAR(255)";
    }

    public String convertBigDecimal() {
        return "DECIMAL";
    }
}
